var dbmodel = require('../dbmodel/dbmodel.js');
var user = dbmodel.model('users');

// 用户验证
exports.userMatch = function(data, pwd, res) {
	let wherestr = {
		$or: [{
			'name': data
		}, {
			'email': data
		}, {
			'wxid': data
		}]
	};
	let out = {
		'name': 1,
		"imgurl": 1,
		'pwd': 1
	}

	user.find(wherestr, out, function(err, result) {
		if (err) {
			res.send({
				status: 500
			});
		} else {
			// 匹配用户名或邮箱是否存在
			if (result == '') {
				res.send({
					status: 404,
					info: '未找到该用户'
				})
			} else {
				// 匹配密码是否正确
				if(pwd == result[0].pwd){
					result.map(function(e) {
						let back = {
							id: e._id,
							name: e.name,
							imgurl: e.imgurl
						}
						res.send({
							status: 200,
							info: '用户匹配成功',
							back
						})
					})
				}else{
					res.send({
						status: 403,
						info: '密码错误'
					})
				}
			}
		}
	})
}

// 用户详情
exports.userDetail = function(id, res) {
	let wherestr = {
		'_id': id
	};
	let out = {
		'psw': 0
	};
	user.findOne(wherestr, out, function(err, result) {
		if (err) {
			res.send({
				status: 500
			});
		} else {
			res.send({
				status: 200,
				result
			});
		}
	})
}